$(function() {
	var duizhangstate;
	var page = 1;
	var tddata=[
		{
			key:'order_no',
			type:1,
			class:'order'
		},{
			key:'ride_time',
			type:1,
			
			id:137
		},{
			key:'departure',
			type:1,
			url:''
		},{
			key:'destination',
			type:1,
			url:''
		},{
			key:'passenger_name',
			type:1,
			url:''
		},{
			key:'passenger_name',
			type:1,
			url:''
		},{
			key:'order_type2',
			type:1,
			url:''
		},{
			key:'use_explain',
			type:1,
			url:''
		},{
			key:'booking_type2',
			type:1,
			url:''
		},{
			key:'driver_id',
			type:1,
			url:''
		},{
			key:'car_number',
			type:1,
			url:''
		},{
			key:'order_money',
			type:1,
			url:''
		},{
			key:'order_state2',
			type:1,
			url:''
		},{
			key:'state2',
			type:4,
			color:'state'
		}];
		var addoperation=``
		
	$('.print').on('click',function(){
		window.print()
	})
	
	var arr;
	accdetail()
	function accdetail(){
		var detailload=layer.load()
	$.ajax({
		type:'post',
		url: "{:url('/adminapi/Check/detail')}",
		data:{
			id:{$Think.get.id}
		},
		success: function(res) {
				$("#loading-first").fadeOut(500);
				if(res.code==1){
				console.log(res)
				$('.company').html(res.data.car_use_name)
				$('.month').html(res.data.month)
				duizhangstate=res.data.check_state
				if(duizhangstate==1){
					$(".account_ok").html("待审核");
					$(".account_ok").attr('disabled','1');
					$('.account_ok').addClass('am-btn-primary tpl-btn-bg-color-success');
					$('.account_ok').attr("disabled","disabled");
				}else if(duizhangstate==2){
					$(".account_ok").html("待审核");
					$(".account_ok").attr('disabled','1');
					$('.account_ok').addClass('am-btn-primary tpl-btn-bg-color-success');
					$('.account_ok').attr("disabled","disabled");
				}else if(duizhangstate==3){
					$(".account_ok").html("确认");
					$(".account_ok").removeClass("layui-btn-disabled ");
					$('.account_ok').addClass('am-btn-primary tpl-btn-bg-color-success');
				}else if(duizhangstate==4){
					$(".account_ok").html("已确认");
					$(".account_ok").removeClass("layui-btn-disabled");
					$('.account_ok').addClass('am-btn-primary tpl-btn-bg-color-success');
					$('.account_ok').attr("disabled","disabled");
				}
				var orderlist=res.data.items
				for(var idx=0;idx<orderlist.length;idx++){
					if(orderlist[idx].state==1){
						orderlist[idx].state2='有效'
					}else{
						orderlist[idx].state2='无效'
					}
					if(orderlist[idx].order_type==1){
						orderlist[idx].order_type2='公务订单'
	//						订单类型,1：，2：企业订单，3：个人订单
					}else if(orderlist[idx].order_type==2){
						orderlist[idx].order_type2='企业订单'
					}else if(orderlist[idx].order_type==3){
						orderlist[idx].order_type2='个人订单'
					}
					
					if(orderlist[idx].booking_type==1){
	//				订车类型：1，。2，。3，
						orderlist[idx].booking_type2='订车（日常）'
					}else if(orderlist[idx].booking_type==2){
						orderlist[idx].booking_type2='包车'
					}else if(orderlist[idx].booking_type==3){
						orderlist[idx].booking_type2='分时租赁'
					}
					
					if(orderlist[idx].order_state==1){
						orderlist[idx].order_state2='待审批'
					}else if(orderlist[idx].order_state==2){
						orderlist[idx].order_state2='已批准'
					}else if(orderlist[idx].order_state==3){
						orderlist[idx].order_state2='已拒绝'
					}else if(orderlist[idx].order_state==4){
						orderlist[idx].order_state2='待用车'
					}else if(orderlist[idx].order_state==5){
						orderlist[idx].order_state2='已派车'
					}else if(orderlist[idx].order_state==6){
						orderlist[idx].order_state2='接驾中'
					}else if(orderlist[idx].order_state==7){
						orderlist[idx].order_state2='已到达出发地'
					}else if(orderlist[idx].order_state==8){
						orderlist[idx].order_state2='已到达目的地'
					}else if(orderlist[idx].order_state==9){
						orderlist[idx].order_state2='待支付'
					}else if(orderlist[idx].order_state==10){
						orderlist[idx].order_state2='待评价'
					}else if(orderlist[idx].order_state==11){
						orderlist[idx].order_state2='已完成'
					}else if(orderlist[idx].order_state==12){
						orderlist[idx].order_state2='已取消'
					}else if(orderlist[idx].order_state==13){
						orderlist[idx].order_state2='已终止'
					}else if(orderlist[idx].order_state==14){
						orderlist[idx].order_state2='无效'
					}
				}
				$('tbody').html(tbodymainf2(orderlist, page, tddata, addoperation, 'id'))
				$('.pagination').html(pagechang1(orderlist.length, 10, page))
	//			$('.order').on('click',function(){
	//				detail($(this).closest('tr').data('id'))
	//			})
				$('.laquo').on('click',function(){
					page-=1;
					console.log(page)
					accdetail()
				})
				$('.pageli').on('click',function(){
					page=$(this).html()
					accdetail()
				})
				//下一页
				$('.raquo').on('click',function(){
					page+=1;
					accdetail()
				});
				console.log(pagechang1(orderlist.length, 10, page))
				layer.close(detailload)
				
			}else{
				layer.close(detailload)
				Err(res.msg)
			}
		},
		error:function(){
			Err('请求失败')
		}
	
	})
	}
	$('.account_ok').on('click',function(){
		var accload=layer.load()
		$.ajax({
			type:"post",
			url:"{:url('adminapi/check/confirm')}",
			data:{
				id:{$Think.get.id}
			},
			success:function(res){
				layer.close(accload)
				console.log(res)
				if(res.code==1){
					Msg('已确认')
					window.history.go(0); 
				}else{
					Err(res.msg)
				}
			}
		});
	})
	$('.btn-back').on('click',function(){
		window.history.back(-1); 
	})
	
	
	function detail(id) {
		console.log(id)
		var load = layer.load(0, {
			shade: [0.3, '#fff']
		});
		
		$.ajax({
			type: "post",
			url: "{:url('detail')}",
			data: {
				order_id: id
			},
			success: function(res) {
				$.ajax({
					type: "post",
					url: "{:url('unit/staff/detail')}",
					data:{
						passenger_id:res.data.passenger_id
					},
					success: function(res1) {
					var limitid=res1.data.limit_id
					
					for(var limitidx=0;limitidx<limitall.length;limitidx++){
						if(limitall[limitidx].limit_id==limitid){
							res.data.limit=limitall[limitidx]
						}
					}
					
				console.log(res)
				res.data.use_type = res.data.use_type - 0 + 1
				if(res.data.use_type == 1) {
					res.data.usetype = '行政执法';
				} else if(res.data.use_type == 2) {
					res.data.usetype = '特殊公务';
				} else if(res.data.use_type == 3) {
					res.data.usetype = '重要会议';
				} else if(res.data.use_type == 4) {
					res.data.usetype = '机要通讯';
				} else if(res.data.use_type == 5) {
					res.data.usetype = '视察调研';
				} else if(res.data.use_type == 6) {
					res.data.usetype = '公务接待';
				} else if(res.data.use_type == 7) {
					res.data.usetype = '应急公务';
				} else if(res.data.use_type == 8) {
					res.data.usetype = '紧急就医';
				} else {
					res.data.usetype = '无用车类型';
				};
				console.log(res)
				var travel_fee;
				var use_time="";
				var count_time=0;
				if(res.data.meter){
					travel_fee=parseInt(res.data.meter/1000*100)/100;
				}else{
					travel_fee=0;
				}
				if(res.data.use_time){
					if(parseInt(res.data.use_time/3600)==0){
						count_time=res.data.use_time
					}else{
						use_time=parseInt(res.data.use_time/3600)+'小时'
						count_time=res.data.use_time-(parseInt(res.data.use_time/3600)*3600)
					}
					console.log(use_time)
					if(parseInt(count_time/60)==0){
						use_time+=count_time+'秒'
					}else{
						use_time+=parseInt(count_time/60)+'分钟';
						use_time+=count_time-(parseInt(count_time/60)*60)+'秒'
					}
//				(res.data.meter/3600;)
				}else{
					use_time=0;
				}
				var wire='········································································';
				layer.close(load);
				var index = layer.open({
					title: false,
					type: 1,
					skin: 'layui-layer-rim', //加上边框
					area: ['50%', '80%'], //宽高
					content: `<div class="am-u-sm-12" style="padding:20px">
							<div class="am-u-sm-12 cl-p">
								<fieldset class="cl-p cl-m">
									<legend>订单详情</legend>
								</fieldset>
							</div>
							<div class="am-u-sm-12 cl-p">
								<div class="am-u-sm-12" style="height:275px;border: 1px solid #ccc;">
									<div id='map' style='width:100%;height:100%'></div>
								</div>
								<div class="am-u-sm-12" style='padding-top:10px'>
									<div class='order_detail_h2'>行程信息</div>
									<div class="am-u-sm-12 cl-p order_detail_text">
										<div class="detail-list">乘车人:</div>${res.data.passenger_name}
									</div>
									<div class="am-u-sm-12 cl-p order_detail_text">
										<div class="detail-list">车型:</div>${res.data.car_type_id}
									</div>
									<div class="am-u-sm-12 cl-p order_detail_text">
										<div class="detail-list">时长:</div>${use_time?use_time:'0'}
									</div>
									<div class="am-u-sm-12 cl-p order_detail_text">
										<div class="detail-list">出发地:</div><div class='am-fl' style='max-width:300px;overflow:auto'>${res.data.departure?res.data.departure:'暂无数据'}</div>
									</div>
									<div class="am-u-sm-12 cl-p order_detail_text">
										<div class="detail-list">目的地:</div><div class='am-fl' style='max-width:300px;overflow:auto'>${res.data.destination?res.data.destination:'暂无数据'}</div>
									</div>
									<div class="am-u-sm-12 cl-p order_detail_text">
										<div class="detail-list">订单编号:</div>${res.data.order_no}
									</div>
									<div class="am-u-sm-12 cl-p order_detail_text">
										<div class="detail-list">下单时间:</div>${res.data.order_time}
									</div>
									<div class="am-u-sm-12 cl-p order_detail_text">
										<div class="detail-list">结束时间:</div>${res.data.complete_time?res.data.complete_time:'暂无数据'}
									</div>
								</div>
								<div class="am-u-sm-12" style='padding-top:10px'>
									<div class='order_detail_h2'>计价信息</div>
									<div class="am-u-sm-12 cl-p order_detail_text">
										<div class="detail-list">里程:</div>${wire}${res.data.travel_fee?res.data.travel_fee:0}元(${travel_fee}公里)
									</div>
									<div class="am-u-sm-12 cl-p order_detail_text">
										<div class="detail-list">时长:</div>${wire}${res.data.time_fee?res.data.time_fee:'0小时'}元(${use_time})
									</div>
									<div class="am-u-sm-12 cl-p order_detail_text">
										<div class="detail-list">财政支付:</div>${wire}${res.data.order_money?res.data.order_money:'0'}元
									</div>
									
								</div>
								<div class="am-u-sm-12"  style='padding-top:10px'>
									<div class='order_detail_h2'>审批信息</div>
									<div class="am-u-sm-12 cl-p order_detail_text">
										<div class="detail-list">申请时间:</div>${res.data.apply_time?res.data.apply_time:'暂无数据'}
									</div>
									<div class="am-u-sm-12 cl-p order_detail_text">
										<div class="detail-list">审批时间:</div>${res.data.approval_time?res.data.approval_time:'暂无数据'}
									</div>
									<div class="am-u-sm-12 cl-p order_detail_text">
										<div class="detail-list">审批员:</div>${res.data.approval?res.data.approval:'无审批员'}
									</div>
									<div class="am-u-sm-12 cl-p order_detail_text">
										<div class="detail-list">用车类型:</div>${res.data.usetype}
									</div>
									<div class="am-u-sm-12 cl-p order_detail_text">
										<div class="detail-list">用车说明:</div>${res.data.use_explain?res.data.use_explain:'无用车说明'}
									</div>
									
								</div>
								<div class="am-u-sm-12" style='padding-top:10px'>
									<div class='order_detail_h2'>用车权限</div>
									<div class="am-u-sm-12 cl-p order_detail_text">
										<div class="detail-list">用车权限:</div>${res.data.limit?res.data.limit.limit_name:'-'}
									</div>
									
								</div>
							</div>
						</div>`,
					success: function(res) {
						var qd = [118.57, 24.82];
						var map = new AMap.Map('map', {
							resizeEnable: true,
							zoom: 13,
							center: qd
						});
						AMapUI.load(['ui/misc/PathSimplifier'], function(PathSimplifier) {
							var pathSimplifierIns = new PathSimplifier({
								zIndex: 100,
								map: map, //所属的地图实例
								getPath: function(pathData, pathIndex) {
									//返回轨迹数据中的节点坐标信息，[AMap.LngLat, AMap.LngLat...] 或者 [[lng|number,lat|number],...]
									return pathData.path;
								},
								getHoverTitle: function(pathData, pathIndex, pointIndex) {
									//返回鼠标悬停时显示的信息
									if(pointIndex >= 0) {
										//鼠标悬停在某个轨迹节点上
										return pathData.name + '，点:' + pointIndex + '/' + pathData.path.length;
									}
									//鼠标悬停在节点之间的连线上
									return pathData.name + '，点数量' + pathData.path.length;
								},
								renderOptions: {
									//轨迹线的样式
									pathLineStyle: {
										strokeStyle: 'red',
										lineWidth: 6,
										dirArrowStyle: true
									}
								}
							});
							var trackload = layer.load(1, {
								shade: [0.1, '#fff'] //0.1透明度的白色背景
							});
							$.ajax({
								type: "post",
								url: "{:url('/admin/order/getTrack')}",
								data: {
									oid:id
								},
								success: function(res) {
									layer.close(trackload);
									var allpath = []
									var speed = 0;
									if(typeof res == 'string') {
										var opath1 = JSON.parse(res).data;
									} else {
										var opath1 = res.data;
									}
									var opath2 = [];
									var opath = [];
									//根据时间进行排序
									if(opath1.length > 1) {
										for(var rankidx = 0; rankidx < opath1.length; rankidx++) {
											opath2.push(opath1[rankidx].time)
										}
										opath2.sort()
										for(var k = 0; k < opath2.length; k++) {
											for(var j = 0; j < opath1.length; j++) {
												if(opath2[k] == opath1[j].time) {
													opath.push(opath1[j])
												}
											}
										}
										console.log(opath)

										for(var oi = 0; oi < opath.length; oi++) {
											var oarr = []
											oarr.push(opath[oi].longitude)
											oarr.push(opath[oi].latitude)
											allpath.push(oarr)
										}
										initPage(PathSimplifier, pathSimplifierIns, map, allpath)
									} else {
										Err('暂无轨迹信息')
									}
								},
								error: function() {
									layer.close(carload);
									Err('请求失败')
								}
							});
						})
					}
				});
				}
				})
			}
		})
	}
})